<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {margin: 0;overflow: hidden;}
    #canvas {background-color: antiquewhite;}
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <script>
    /** @type {HTMLCanvasElement} */
    const canvas = document.querySelector('#canvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    const ctx = canvas.getContext('2d');

    const img = new Image();
    img.src = './images/wns.jpg';
    img.onload = render;

    // 色块尺寸
    let size = 10;

    function render() {
      // 图像尺寸
      const { width, height } = img;
      ctx.drawImage(img, 0, 0);
      const imgDt = ctx.getImageData(0,0,width,height);
      const data = imgDt.data;

      /* 行列遍历 */
      for (let y = 0; y < height; y+=size) {
        for (let x = 0; x < width; x+=size) {
          const i = (y*width+x)*4;
          let [r,g,b,a] = [
            data[i],
            data[i+1],
            data[i+2],
            data[i+3]
          ];
          const color = `rgba(${r},${g},${b},${a})`;
          // 绘制色块
          ctx.fillStyle = color;
          ctx.fillRect(x, y, size, size);
        }
      }
    }

  </script>
</body>
</html>